package interview.stringpermutation;

import java.util.ArrayList;
import java.util.Iterator;

public class StringPermutation {
	public ArrayList<String> permutation(char[] src, int start, int end) {
		ArrayList<String> result = new ArrayList<String>();

		if(start == end) {
			result.add(new String(src, start, end - start + 1));
			return result;
		}

		ArrayList<String> p;
		for(int i = start; i <= end; i++) {
			char temp = src[start];
			src[start] = src[i];
			src[i] = temp;
			p = permutation(src, start + 1, end);
			Iterator<String> it = p.iterator();
			while(it.hasNext()) {
				result.add(src[start] + it.next());
			}
			temp = src[start];
			src[start] = src[i];
			src[i] = temp;
		}

		return result;
	}
}
